#!/bin/bash
set -e;
source ../../pi.vars;
sudo apt install aria2 -y;
whoname=`whoami`
sudo mkdir -p /download;
sudo mkdir -p /opt/aria2;sudo chmod -R 755 /opt/aria2;
sudo chown -R ${whoname}  /opt/aria2 /download;
cp aria2.* /opt/aria2/;
cp update* /opt/aria2/;
cp dht.dat /opt/aria2/;
cp *.sh /opt/aria2/;
sed -i "s/User=.*/User=${whoname}/g" /opt/aria2/aria2.service;
sudo cp /opt/aria2/aria2.service /lib/systemd/system/aria2.service;
sudo systemctl daemon-reload;
sudo systemctl enable --now aria2;
sudo systemctl start aria2;
echo "### connection info ###";
cat /opt/aria2/aria2.config |grep rpc-secret; 
cat /opt/aria2/aria2.config |grep rpc-listen-port; 






echo "start install web ui......"
sudo apt install nginx -y;
AriaNg_version=`get_github_latest https://github.com/mayswind/AriaNg/releases/latest`



whoname=`whoami`
sudo chown -R ${whoname} /var/www/html;
rm -rf  /var/www/html/AriaNg*;
mkdir -p /var/www/html/AriaNg;
wget ${github_proxy}https://github.com/mayswind/AriaNg/releases/download/${AriaNg_version}/AriaNg-${AriaNg_version}-AllInOne.zip -O /var/www/html/AriaNg/AriaNg-AllInOne.zip
cd  /var/www/html/AriaNg;
unzip AriaNg*.zip ;rm -f AriaNg*.zip;


sudo systemctl enable --now nginx;

echo "web ui: http://ip/AriaNg"